package edu.scut.course_management.dto;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import edu.scut.course_management.exception.CustomException;
import edu.scut.course_management.po.Attachment;
import edu.scut.course_management.po.Homework;
import edu.scut.course_management.po.Section;
import edu.scut.course_management.po.SectionHomework;
import edu.scut.course_management.result.ResultCode;

/**
 * 用于封装发布作业接口传输的数据
 * @author huaming
 */
public class NewHomeworkDTO {
    private Integer periodId;
    private String year;
    private String term;
    private Integer courseId;
    private String buildingId;
    private String roomNumber;
    private String homeworkTitle;
    private String homeworkContent;
    private String deadline;
    private List<Attachment> attachmentList;
  
    public Integer getPeriodId() {
        return periodId;
    }
   
    public void setPeriodId(Integer periodId) {
        this.periodId = periodId;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getTerm() {
        return term;
    }

    public void setTerm(String term) {
        this.term = term;
    }

    public int getCourseId() {
        return courseId;
    }

    public void setCourseId(int courseId) {
        this.courseId = courseId;
    }

    public String getBuildingId() {
        return buildingId;
    }

    public void setBuildingId(String buildingId) {
        this.buildingId = buildingId;
    }

    public String getRoomNumber() {
        return roomNumber;
    }

    public void setRoomNumber(String roomNumber) {
        this.roomNumber = roomNumber;
    }

    public String getHomeworkTitle() {
        return homeworkTitle;
    }

    public void setHomeworkTitle(String homeworkTitle) {
        this.homeworkTitle = homeworkTitle;
    }

    public String getDeadline() {
        return deadline;
    }

    public void setDeadline(String deadline) {
        this.deadline = deadline;
    }

    public String getHomeworkContent() {
        return homeworkContent;
    }

    public void setHomeworkContent(String homeworkContent) {
        this.homeworkContent = homeworkContent;
    }

    public void setAttachmentList(List<Attachment> attachmentList) {
        this.attachmentList = attachmentList;
    }

    public List<Attachment> getAttachmentList() {
        return attachmentList;
    }

    public Homework getHomework() throws CustomException {
        Homework homework = new Homework();
        homework.setHomeworkTitle(homeworkTitle);
        homework.setHomeworkContent(homeworkContent);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 设置当前时间为发布时间
        Date publishTime = new Date();
        homework.setPublishTime(publishTime);
        // 将截至日期的字符串转为时间类型
        Date deadlineTime = null;
        try {
             deadlineTime = dateFormat.parse(deadline);
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new CustomException(ResultCode.PARAM_NOT_COMPLETE);
        }
        homework.setDeadline(deadlineTime);
        return homework;
    } 
    
    /**
     * 从发布作业接口传入的dto对象中截取出section_homework需要的字段
     * 这里是缺省homework_id的，需要插入homework表格后才能获取
     * @return
     */
    public SectionHomework getSectionHomework() {
        SectionHomework sectionHomework = new SectionHomework();
        sectionHomework.setPeriodId(periodId);
        sectionHomework.setRoomNumber(roomNumber);
        sectionHomework.setBuildingId(buildingId);
        sectionHomework.setCourseId(courseId);
        sectionHomework.setTerm(term);
        sectionHomework.setYear(year);
        return sectionHomework;
    }

    public List<Section> getSectionList() {
        List<Section> sections = new ArrayList<Section>();
        Section temp = new Section();
        temp.setBuildingId(buildingId);
        temp.setCourseId(courseId);
        temp.setPeriodId(periodId);
        temp.setRoomNumber(roomNumber);
        temp.setTerm(term);
        temp.setYear(year);
        sections.add(temp);
        return sections;
    }

    /**
     * 对传入的各字段作非空检验
     * @return
     */
    public boolean notNull() {
        return !(periodId == null || roomNumber == null || buildingId == null || courseId == null
        || term == null || year == null || homeworkTitle == null || homeworkContent == null || deadline == null);
    }    

}
